package com.hdoit.ares.web.acc.apis;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.acc.model.IncomeDetail;
import com.hdoit.ares.logical.acc.service.IIncomeDetailService;
import com.hdoit.ares.logical.viewmodel.acc.FindIncomeDetail;
import com.tennetcn.component.webapi.BaseApi;
import com.tennetcn.component.webapi.ResponseModel;
import com.tennetcn.component.webapi.ResponseStatus;

@Controller
@RequestMapping(value = "/api/v1/IncomeDetail/",method = {RequestMethod.GET,RequestMethod.POST},produces="application/json;charset=utf-8")
@Api(value="/api/v1/IncomeDetail/")
@CrossOrigin(origins = "*",maxAge = 3600)
public class IncomeDetailAresApi extends BaseApi{
	
	private IIncomeDetailService incomeDetailService;
	public IIncomeDetailService getIncomeDetailService() {
		return incomeDetailService;
	}
	@Resource
	public void setIncomeDetailService(IIncomeDetailService incomeDetailService) {
		this.incomeDetailService = incomeDetailService;
	}
	
	private final String incomeListApi="incomeList";
	private final String incomeApi="income";
	
	
	@ResponseBody
	@RequestMapping(value=incomeListApi)
	@ApiOperation(notes = incomeListApi, httpMethod = "POST", value = "获取奖金详情列表信息")
	public String IncomeList(FindIncomeDetail findIncomeDetail,
			@ApiParam("第几页,必选") @RequestParam(value="pageNum",required=true)Integer pageNum,
			@ApiParam("每页数量") @RequestParam(value="length",required=true)Integer length
			/*@ApiParam("查询的开始时间") @RequestParam(value="strDate")String strDate,
			@ApiParam("查询的结束时间") @RequestParam(value="endDate")String endDate*/){
		/*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
	    try {
	    	if(!StringUtils.isEmpty(strDate)){
	    		findIncomeDetail.setStartDate(sdf.parse(strDate));
	    	}
	    	if(!StringUtils.isEmpty(endDate)){
	    		findIncomeDetail.setEndDate(sdf.parse(endDate));
	    	}
		} catch (ParseException e) {
			e.printStackTrace();
		}*/
	    
		ResponseModel rm=new ResponseModel();
		String custId=getLoginId();
		int start=(pageNum-1)*length;
		
		List<IncomeDetail> incomeDetails = incomeDetailService.queryIncomeList(findIncomeDetail, custId,start,length);
		
		if(incomeDetails.size()>0){
			rm.setStatus(ResponseStatus.success);
			rm.setMessage("ok");
			List<String> params = new ArrayList<String>();
			Field[] fields = IncomeDetail.class.getDeclaredFields();
			for(Field field : fields){
				params.add(field.getName());
			}
			
			rm.putList("incomeDetail", incomeDetails, params);
		}else{
			rm.setMessage("没有查询到数据");
		}

		Double incomeMoney = null;
		IncomeDetail incomeDetail1 = incomeDetailService.querySumIncomeMoney(findIncomeDetail, custId);
		if (incomeDetail1 == null) {
			incomeMoney = 0.00;
		} else {
			incomeMoney = incomeDetail1.getIncomeMoney();
		}		
		if(incomeMoney==null){
			rm.setMessage("总收入计算错误");
		}else{
		    rm.put("incomeMoney", incomeMoney);
		}
		
		Double expendMoney = null;
		IncomeDetail incomeDetail2 = incomeDetailService.querySumExpendMoney(findIncomeDetail, custId);
		if (incomeDetail2 == null) {
			expendMoney = 0.00;
		} else {
			expendMoney = incomeDetail2.getExpendMoney();
		}
		if(expendMoney==null){
			rm.setMessage("总支出计算错误");
		}else{
			rm.put("expendMoney", expendMoney);
		}
		
		return rm.toJson();
	}
	
	@ResponseBody
	@RequestMapping(value= incomeApi)
	@ApiOperation(notes= incomeApi,httpMethod= "POST",value= "选择的奖金详情页面")
	public String income(String id) {

		ResponseModel rm=new ResponseModel();
		if(StringUtils.isEmpty(id)){
			rm.setMessage("id为空，请输入id");
		}
		
		IncomeDetail incomeDetail = incomeDetailService.queryIncomeDetail(id);
		rm.put("incomeDetail", incomeDetail);

		return rm.toJson();
	}
}
